<?php
/**
 * User: Drery
 * Date: 2015/12/16
 * Time: 17:31
 */

namespace backend\components;

use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use yii\base\Object;

class QiniuComponents extends Object
{
    /** @var Auth*/
    public $auth;
    public $accessKey;
    public $secretKey;
    public $bucket;

    public function init()
    {
        $this->accessKey = 'y4BBjCnUfHf-MSQ1fcDe1UgvPJwSG-yJse6VESSK';
        $this->secretKey = 'nt1aI5CU5MzFO4Bd6AzHSGgtpTWjFEbZec4MYSWX';
        $this->bucket = 'zxc-story';
        $this->auth = new Auth($this->accessKey, $this->secretKey);
    }

    public function getUploadToken($options = [])
    {
        $bucket = isset($options['bucket']) ? $options['bucket'] : $this->bucket;
        $key = isset($options['key']) ? $options['key'] : null;
        $expires = isset($options['expires']) ? $options['expires'] : 3600;
        $policy = isset($options['policy']) ? $options['policy'] : null;
        return $this->auth->uploadToken($bucket,$key,$expires,$policy);
    }

    public function deleteFile($key, $bucket = null)
    {
        $manager = new BucketManager($this->auth);
        $bucket = empty($bucket) ? $this->bucket : $bucket;
        return $manager->delete($bucket, $key);
    }
}